<script setup>
import { reactive, ref,onMounted } from 'vue';
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

// 引入axios编写api
import loginApi from '../api/login.js'
import captchaApi from '../api/captcha.js';

const admin = ref({
  phone: '',
  password: '',
  captcha: '',
  key: ''
});
onMounted(function(){
  // 加载验证码
  loadCaptcha();
})

// 获取路由对象
const router = useRouter();

// 验证码图片数据
const captcha = ref("");


 const onSubmit=async  ()=> {
  const res=await loginApi.dologin(admin.value);
  const arr=res.split(".");
  if(arr.length==3){
    router.push("/");
  }
}

// 加载验证码
const loadCaptcha=async ()=>{
    const res=await captchaApi.captcha();
    admin.value.key=res.key;
    captcha.value=res.base;
}

</script>


<template>
  <div class="login-bg">
    <el-form class="form-login" :model="admin">
      <el-row>
        <el-col :span="10">
          <img style="width: 100px" src="@/assets/logo.png" alt="中享思途" />
        </el-col>
        <el-col :span="14">
          <h1>系统登录</h1>
        </el-col>
      </el-row>
      <el-form-item>
        <el-input v-model="admin.phone"
                  placeholder="请输入手机号"
                  :prefix-icon="User"
                  maxlength="16"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="admin.password"
                  placeholder="请输入密码"
                  show-password
                  :prefix-icon="Lock"
                  maxlength="16"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="admin.captcha"
                  placeholder="验证码"
                  :prefix-icon="Key"
                  maxlength="5"
                  class="input-verCode"></el-input>
        <img :src="captcha"
             class="img-verCode" @click="loadCaptcha">
      </el-form-item>
      <el-form-item>
        <el-button class="btn-login" type="success" @click="onSubmit">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped>

.login-bg{
  height: 100%;
  background-image: url('../assets/back.jpg');
  background-repeat: no-repeat;
  background-position: center;
  background-attachment: fixed;
  background-size: cover;
}

.form-login{
  width: 280px;
  padding: 20px;
  position: absolute;
  top: 20%;
  left: calc(50% - 150px);
  background-color: #FFF;
  box-shadow: 10px 10px 30px #000;
}

.btn-login{
  width: 100%;
}
h1{
  text-align: center;
  font-size: 24px;
  margin-bottom: 18px;
  height: 50px;
  line-height: 50px;
}
.input-verCode{
  width: 160px;
}
.img-verCode{
  width: 80px;
  height:30px;
}
</style>